package org.jack.activity;

import org.jack.bluetooth.BluetoothClient;
import org.jack.input.SwitchEvent;
import org.jack.view.House;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;

public class MobileController extends Activity implements OnClickListener
{
	
	private BluetoothClient client = null;
	private House house = null;
	private final static String TAG = MobileController.class.getSimpleName();
	private WakeLock wakeLock;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Log.d(TAG, "Build bluetooth client");
		client = new BluetoothClient(this);
		house = new House(this,client);	
        setContentView(house);
        //Control the power
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE ); 
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK , TAG); 
    }

	@Override
	protected void onResume() 
	{
		super.onResume();
		wakeLock.acquire();
		house.resume();
		if (!client.isConnected())
		{
			client.connect();
		}
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		wakeLock.release();
		house.pause();
		if (isFinishing())
		{
			client.close();
			house.dispose();
		}
	}
	
	@Override
	public void onClick(View v)
	{
		int msg = 0;
		switch (v.getId())
		{
		case R.id.open_left_light:
			msg = SwitchEvent.OPEN_LEFT_LIGHT;
			break;
		case R.id.close_left_light:
			msg = SwitchEvent.CLOSE_LEFT_LIGHT;
			break;
		case R.id.open_right_light:
			msg = SwitchEvent.OPEN_RIGHT_LIGHT;
			break;
		case R.id.close_right_light:
			msg = SwitchEvent.CLOSE_RIGHT_LIGHT;
			break;
		case R.id.low_gear_fan:
			msg = SwitchEvent.LOW_GEAR_FAN;
			break;
		case R.id.high_gear_fan:
			msg = SwitchEvent.HIGH_GEAR_FAN;
			break;
		case R.id.close_fan:
			msg = SwitchEvent.CLOSE_FAN;
			break;
		case R.id.click_door:
			msg = SwitchEvent.CLICK_DOOR;
			break;
			default:break;
		}
		client.sendMsg(msg);
	}
	
  
	
}